import { Injectable } from '@angular/core';
import { Platform, ToastController, App, NavController, Tabs ,IonicApp} from 'ionic-angular';
import { TabsPage } from '../pages/tabs/tabs';
import { HomePage } from "../pages/home/home";
import { AboutPage } from "../pages/about/about";
import { ShopSearchPage } from "../pages/shop-search/shop-search";
import { ContactPage } from "../pages/contact/contact";
import { PersonalPage } from "../pages/personal/personal";
// import { LoginPage } from '../pages/login /login';
import { LoginPage } from "./../pages/login/login";
@Injectable()
export class BackButtonService {

    checkPage: boolean;
    //控制硬件返回按钮是否触发，默认false
    backButtonPressed: boolean = false;

    //构造函数 依赖注入
    constructor(public platform: Platform,
        public appCtrl: App,
        private ionicApp:IonicApp,
        public toastCtrl: ToastController) { }


    // 判断当前页面
   
    //注册方法
    registerBackButtonAction(tabRef: Tabs): void {
        console.log(tabRef)
    
        //registerBackButtonAction是系统自带的方法
        this.platform.registerBackButtonAction(() => {
          //获取NavController
          let activeNav: NavController = this.appCtrl.getActiveNavs()[0];
          let activePortal = this.ionicApp._modalPortal.getActive() ||this.ionicApp._overlayPortal.getActive();
                let loadingPortal = this.ionicApp._loadingPortal.getActive();
                if (activePortal) {
                    //其他的关闭
                    activePortal.dismiss().catch(() => {
                    });
                    activePortal.onDidDismiss(() => {
                    });
                    return;
                }
          //如果可以返回上一页，则执行pop
          if (activeNav.canGoBack()) {
            activeNav.pop();
          } else {
            if (tabRef == null || tabRef._selectHistory[tabRef._selectHistory.length - 1] === tabRef.getByIndex(0).id) {
              //执行退出
              this.showExit();
            } else {
              //选择首页第一个的标签
              tabRef.select(0);
            }
          }
        });
      }


    // registerBackButtonAction(tabRef: Tabs, callback): void {

    //     //registerBackButtonAction是系统自带的方法
    //     this.platform.registerBackButtonAction(() => {
    //         // this.goBackLogic();
    //         console.log("监听右键Boolean值：" + this.checkPage);
    //         let activeNav: NavController = this.appCtrl.getActiveNavs()[0];
    //         console.log(activeNav)
    //         if (!activeNav.canGoBack()) {
    //             if (tabRef == null) {
    //                 callback;
    //             }else{
    //                 this.showExit();
    //             }
    //           //如果是根目则按照需求1处理
              
    //         } else {
    //           //非根目录返回上一级页面
    //           this.appCtrl.goBack();
    //         }
           
    //         }, 10);
    // }

    //退出应用方法
    private showExit(): void {
        //如果为true，退出
        if (this.backButtonPressed) {
            this.platform.exitApp();
        } else {
            //第一次按，弹出Toast
            this.toastCtrl.create({
                message: '再按一次退出应用',
                duration: 2000,
                position: 'top'
            }).present();
            //标记为true
            this.backButtonPressed = true;
            //两秒后标记为false，如果退出的话，就不会执行了
            setTimeout(() => this.backButtonPressed = false, 2000);
        }
    }
}